import {defineConfig, loadEnv} from 'vite';
import {fileURLToPath, URL} from 'node:url';
import react from '@vitejs/plugin-react';
import tailwindcss from "@tailwindcss/vite";


export default ({mode}: { mode: Parameters<typeof loadEnv>[0] }) => {

    const env: Record<string, string> = loadEnv(mode, process.cwd());

    return defineConfig({
        server: {
            host: '0.0.0.0',
            cors: true,
            port: 8080,
            proxy: {
                '/api': {
                    target: env.VITE_APP_LOCAL_URL,
                    changeOrigin: true,
                    ws: false,
                    rewrite: (path: string) => path.replace(/^\/api/, ""),
                }
            }
        },
        plugins: [
            react(),
            tailwindcss()
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            }
        },
        build: {
            sourcemap: false,
            minify: 'terser',
            chunkSizeWarningLimit: 2000,
            terserOptions: {
                compress: {
                    drop_console: true,
                    drop_debugger: true,
                    properties: true,
                    keep_fnames: true
                }
            },
            rollupOptions: {
                output: {
                    chunkFileNames: "assets/js/[name]-[hash].js",
                    entryFileNames: "assets/js/[name]-[hash].js",
                    assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
                }
            }
        }
    })
}
